<?php

    include("header.php");

    setTabIndex(2);

    createPage("Liga - Zilveren Toren");

?>



<head>

<style>

.clsTableTitle {

	font-family: Verdana, "MS Sans Serif";

	font-size: 8pt;

	font-weight: bold;

	color: #E0FFFF;

	text-align: left;

	border: 1px inset;

	background-image: url(/assets/images/bars/winbar_blue.jpg);

}

</style>

</head>

<?php

	if (isset($_GET['dep'])) {

  	$dep = $_GET['dep'];

	} else if (isset($_SESSION['dep'])) {

	  $dep = $_SESSION['dep'];

	}



	if (isset($_GET['round'])) {

  	$round = $_GET['round'];

	} else if (isset($_SESSION['round'])) {

	  $round = $_SESSION['round'];

	}



	if ( empty($dep) || empty($round)) {

	  rejectIfError(null, "view_game_list.php: afdeling of ronde kon niet worden gevonden.");

	}



	if (isset($_SESSION['user'])) {

		$user = $_SESSION['user'];

	}



	$connect = mysql_connect(DB_SERVER, DB_USER, DB_PWD);

	mysql_select_db(DB_CATALOG);



?>



<table height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0" bgcolor="#FFFFFF">

	<tr>

		<td id="leftFiller" valign="top" height="100%" rowspan="3" bgcolor="#666666">

			<table border="0" cellpadding="0" cellspacing="0" height="100%" width="8px">

				<tr><td>&nbsp;</td></tr>

			</table>

		</td>

		<td id="header" valign="top" width="100%">

			<table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#10425b">
                <tr>
                    <td bgcolor="#666666" width="40%">
                        <?php include("inc_title.html"); ?>
                    </td>
                    <td bgcolor="#666666" align="right" valign="top">&nbsp;
                          <span class="headerSmall">Huidige versie PLAYER.DBF: <?=CURRENT_PLAYER_DBF?>
                        <?php
                        if (isset($user) && $user->club_id == 100) {
                        ?>
                        <br/>
                        <a class="headerSmall" href="import_player.php">laad andere versie</a>
                        <?php
                        }
                        ?>
                        </span>
                    </td>
                </tr>
            </table>
            <table align="center" border="0" width="100%" height="90%" cellpadding="0" cellspacing="0" style="border-top:#BCDD10 4px solid;">
                <tr>
                    <?php include("inc_sublinks.php"); ?> 
                </tr>

				<tr><td colspan="2" nowrap bgcolor="#BCDD10" height="4"><img src="../../assets/images/spacer.gif" alt="" height="1" width="1"></td></tr>

				<tr>

					<td>&nbsp;</td>

					<td valign="top">

						<table align="left" cellspacing="10" width="100%">

							<tr>

								<td colspan="4">

                  <span class="blackHeaderLarge">Zilveren Toren <?=CURRENT_ZT_YEAR?>

                  - Afdeling <?=$dep?> - Partijen</span>

                  <br>&nbsp;<br>

                  <?php

                  $query = "select g.group_name from zt_group g join

		          	zt_tournament zt on zt.tournament_id = g.tournament_id

		            where zt.isCurrent = 1";

		          $result = mysql_query($query, $connect);

		          while ($row = mysql_fetch_assoc($result)) {

                  	$groupName = $row['group_name'];

                  ?>

                  <a class="quicklink" href="zt_department.php?dep=<?=$groupName?>">Afdeling <?=$groupName?></a>|</li>

                  <?php

                  }

                  ?>

                  <br>&nbsp;<br>

								</td>

							</tr>

							<tr>

  							<td align="center" colspan="4"><a href="zt_department.php?dep=<?=$dep?>"><img border="0" src="../../assets/images/24x24/shadow/arrow_left_green.gif"></a></td>

							</tr>

							<tr align="center">

								<td colspan="6" class="clsTableData">

  								<?php

    								$query = sprintf("select g.nrOfRounds from zt_group g

                      join zt_tournament t on t.tournament_id = g.tournament_id

                      where g.group_name = '%s' and t.tournament_name ='%s'",

                      $dep, CURRENT_ZT_TOURNAMENT);

                    $result = mysql_query($query, $connect);

                    rejectIfError($query, "view_game_list.php: selecteren aantal rondes is mislukt.");

                    $row = mysql_fetch_assoc($result);

                    $nrOfRounds = $row['nrOfRounds'];



                    for ($i = 1; $i <= $nrOfRounds; $i++) {

                      if ($i == $round) {

                  ?>

                        <a href="view_game_list.php?dep=<?=$dep?>&round=<?=$i?>"

                          style="color: #069;background-color: #cfc;"><?=$i?></a>

      									&nbsp;&nbsp;&nbsp;

                  <?php

                      } else {

                  ?>

                        <a href="view_game_list.php?dep=<?=$dep?>&round=<?=$i?>"><?=$i?></a>

      									&nbsp;&nbsp;&nbsp;

                  <?php

                      }

  									}

									?>

								</td>

							</tr>

							<tr>

  							<td>&nbsp;</td>

  							<td width="90%">

  							  <table id="tab" border="0" width="90%" cellspacing="0" cellpadding="0" class="default">

    								<tr>

    								  <?php

      								  $query = "select DATE_FORMAT(c.earliest_date,'%e/%m/%Y') AS earliest_date,

        								  DATE_FORMAT(c.latest_date,'%e/%m/%Y') AS latest_date

                          from zt_calendar c join zt_tournament t on c.tournament_id = t.tournament_id

                          where c.round_nr = ".$round." and t.tournament_name =

                          '".CURRENT_ZT_TOURNAMENT."'";

                        $result = mysql_query($query, $connect);

                        rejectIfError($query, "view_game_list.php: kalendargegevens is mislukt.");

                        $row = mysql_fetch_assoc($result);

                        $start = $row['earliest_date'];

                        $end = $row['latest_date'];

    								  ?>

                      <td colspan="9" class="clsTableTitle">RONDE <?=$round?>

                      :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                      <?php

                      if ($start != $end) {

                      ?>

                      van <?=$start?> tot <?=$end?></td>

                      <?php

                      } else {

                      ?>

                      <?=$start?>

                      <?php

                      }

                      ?>

    								</tr>

    								<?php

      								$query = sprintf("select g.* from zt_game g join zt_calendar c on

                        c.calendar_id = g.calendar_id join zt_tournament t on

                        c.tournament_id = t.tournament_id join zt_group gr

                        on gr.group_id = g.group_id where c.round_nr = %u

                        and t.tournament_name = '%s' and gr.group_name = '%s'",

                        $round, CURRENT_ZT_TOURNAMENT, $dep);

                      $result = mysql_query($query, $connect);

                      rejectIfError($query, "view_game_list.php: selecteren partijgegevens is mislukt.");

                      if (mysql_num_rows($result) > 0) {

                        while ($row = mysql_fetch_assoc($result)) {

                          $gameId = $row['game_id'];



                          $query = "select team_name, club_id from zt_team where team_id = ".$row['home_team'];

                          $result2 = mysql_query($query, $connect);

                          rejectIfError($query, "view_game_list.php: selecteren naam thuisploeg is mislukt.");

                          $row2 = mysql_fetch_assoc($result2);

                          $homeTeam = $row2['team_name'];

                          $homeTeamClub = $row2['club_id'];



                          $query = "select team_name, club_id from zt_team where team_id = ".$row['visit_team'];

                          $result2 = mysql_query($query, $connect);

                          rejectIfError($query, "view_game_list.php: selecteren naam bezoekersploeg is mislukt.");

                          $row2 = mysql_fetch_assoc($result2);

                          $visitTeam = $row2['team_name'];

                          $visitTeamClub = $row2['club_id'];

                    ?>

                        <tr>

        								  <td colspan="9" height="5%">&nbsp;</td>

        								</tr>

        								<tr>

          								<td>&nbsp;</td>

        								  <td width="50%" colspan="4" align="right" class="blue"><b><?=$homeTeam?>&nbsp;&nbsp;</b></td>

        								  <td colspan="4" align="left" class="blue"><b>-&nbsp;&nbsp;<?=$visitTeam?></b></td>

        								</tr>

        								<tr>

        								  <td colspan="9">&nbsp;</td>

        								</tr>

        								<?php

        								$query = "SELECT gp.game_player_id, gp.board_nr, gp.index_nr_home 

        									AS hindex, gp.home_error_id AS herror, 

        									g.home_player_list_id AS hpl_id, g.visit_player_list_id AS vpl_id, 

        									gp.visit_error_id AS verror, gp.index_nr_visit AS vindex, rt.result

											FROM zt_game_player gp

											JOIN zt_game g ON g.game_id = gp.game_id

											JOIN zt_result_type rt ON rt.result_type_id = gp.result_type_id

											WHERE gp.game_id = ".$gameId."

											ORDER BY gp.board_nr";

        								

          								/*$query = "SELECT gp.board_nr, hp.index_nr as hindex, hp.player_id as hpId,

				                            hp.player_key as hpKey, vp.index_nr as vindex, vp.player_id as vpId,

				                            vp.player_key as vpKey, rt.result

				                            FROM zt_game_player gp

				                            JOIN zt_game g ON g.game_id = gp.game_id

				                            JOIN zt_team ht ON ht.team_id = g.home_team

				                            JOIN club hc ON hc.club_id = ht.club_id

				                            JOIN zt_player_list hpl ON hpl.club_id = ht.club_id

				                            JOIN zt_player hp ON hp.player_list_id = hpl.player_list_id

				                            JOIN zt_team vt ON vt.team_id = g.visit_team

				                            JOIN club vc ON vc.club_id = vt.club_id

				                            JOIN zt_player_list vpl ON vpl.club_id = vt.club_id

				                            JOIN zt_player vp ON vp.player_list_id = vpl.player_list_id

				                            JOIN zt_result_type rt on rt.result_type_id = gp.result_type_id

				                            WHERE gp.game_id = ".$gameId."

				                            AND gp.index_nr_home = hp.index_nr

				                            AND gp.index_nr_visit = vp.index_nr

				                            AND hpl.date_approved is not null

				                            AND vpl.date_approved is not null

				                            ORDER BY gp.board_nr";*/

          								$result3 = mysql_query($query, $connect);

          								rejectIfError($query, "view_game_list.php: selecteren detail partijgegevens is mislukt.");

          								while ($row3 = mysql_fetch_assoc($result3)) {

            								$gamePlayerId = $row3['game_player_id'];

            								$homePlayerListId = $row3['hpl_id'];

            								$visitPlayerListId = $row3['vpl_id'];

          									$boardNr = $row3['board_nr'];

            								$matchResult = $row3['result'];

            								$hindex = $row3['hindex'];

            								$vindex = $row3['vindex'];

            								$herror = $row3['herror'];

            								$verror = $row3['verror'];

            								$matchResult = str_replace("5", "&frac12;", $matchResult);



				                            $homePlayer = new Player();

            								if ($hindex != 0) {

				                            	$query = "SELECT p.nom_prenom, p.matricule, p.elo_calcul FROM zt_game g

													JOIN zt_game_player gp ON gp.game_id = g.game_id

													JOIN zt_team ht ON ht.team_id = g.home_team

													JOIN club hc ON hc.club_id = ht.club_id

													JOIN zt_player_list hpl ON hpl.club_id = ht.club_id

													JOIN zt_player hp ON hp.player_list_id = hpl.player_list_id

													JOIN playernew p ON p.matricule = hp.player_key

													WHERE gp.game_player_id = ".$gamePlayerId."

													AND hpl.player_list_id = ".$homePlayerListId."

													AND gp.index_nr_home = hp.index_nr

													AND hpl.date_approved IS NOT NULL

													ORDER BY gp.board_nr";

				                            	$playerDetails = mysql_query($query, $connect);

				                            	$playerDetailsRow = mysql_fetch_assoc($playerDetails);

				                            	$homePlayer->index_nr = $hindex;

				                            	$homePlayer->player_name = $playerDetailsRow['nom_prenom'];

				                            	$homePlayer->elo = $playerDetailsRow['elo_calcul'];

                                                $homePlayer->player_key = $playerDetailsRow['matricule'];

				                            } else {

				                            	$homePlayer->index_nr = "";

				                            	$homePlayer->player_name = "<label style='color:#990000'>FORFAIT</label>";

				                            	$homePlayer->elo = "";

				                            }

				                            

          									$visitPlayer = new Player();

            								if ($vindex != 0) {

				                            	$query = "SELECT p.nom_prenom, p.matricule, p.elo_calcul FROM zt_game g

													JOIN zt_game_player gp ON gp.game_id = g.game_id

													JOIN zt_team vt ON vt.team_id = g.visit_team

													JOIN club vc ON vc.club_id = vt.club_id

													JOIN zt_player_list vpl ON vpl.club_id = vt.club_id

													JOIN zt_player vp ON vp.player_list_id = vpl.player_list_id

													JOIN playernew p ON p.matricule = vp.player_key

													WHERE gp.game_player_id = ".$gamePlayerId."

													AND vpl.player_list_id = ".$visitPlayerListId."

													AND gp.index_nr_visit = vp.index_nr

													AND vpl.date_approved IS NOT NULL

													ORDER BY gp.board_nr";

				                            	$playerDetails = mysql_query($query, $connect);

				                            	$playerDetailsRow = mysql_fetch_assoc($playerDetails);

				                            	$visitPlayer->index_nr = $vindex;

				                            	$visitPlayer->player_name = $playerDetailsRow['nom_prenom'];

				                            	$visitPlayer->elo = $playerDetailsRow['elo_calcul'];

                                                $visitPlayer->player_key = $playerDetailsRow['matricule'];

				                            } else {

				                            	$visitPlayer->index_nr = "";

				                            	$visitPlayer->player_name = "<label style='color:#990000'>FORFAIT</label>";

				                            	$visitPlayer->elo = "";

				                            }

        								?>

        								<tr id="<?=$homeTeamClub."-".$visitTeamClub?>">

          								<?php

            								if ( isset($user) && $user->club_id != 100

                            					&& ($user->club_id == $homeTeamClub

                            					|| $user->club_id == $visitTeamClub)) {

            							?>

              								<td><!-- <a href="complain_game_board.php?dep=<?=$dep?>&game_id=<?=$gameId?>&board_nr=<?=$boardNr?>"><img src="/assets/images/16x16/shadow/exclamation.gif" border="0" alt="Betwist dit resultaat"/></a> --></td>

            							<?php

            								} else {

                        				?>

				                          	<td>&nbsp;</td>

				                        <?php

				                          	}

				          				?>

				

				                          	<td width="10%"><?=$boardNr?></td>

				        					<td width="5%" align="right"><?=$homePlayer->index_nr?>&nbsp;&nbsp;</td>

				                          	<td width="25%"><a class="underline" href="player_detail.php?dep=<?=$dep?>&playerKey=<?=$homePlayer->player_key?>"><?=$homePlayer->player_name?></a></td>

				                          	<td width="10%" style="border-right:#DDDDDD solid 1px"><?=$homePlayer->elo?></td>

				                          	<td width="5%" align="right"><?=$visitPlayer->index_nr?>&nbsp;&nbsp;</td>

				                          	<td width="25%"><a class="underline" href="player_detail.php?dep=<?=$dep?>&playerKey=<?=$visitPlayer->player_key?>"><?=$visitPlayer->player_name?></a></td>

				                          	<td width="10%"><?=$visitPlayer->elo?></td>

				                          	<td align="center"><?=$matchResult?></td>

        								</tr>

					                    <?php

					                    

					                    	$herrorDesc = "";

					                    	$verrorDesc = "";

					                    

					                    	if ($herror != 0) {

					                    		$query = "select error_desc from zt_game_player_error

					                    			where error_id = ".$herror;

				                            	$errorDetails = mysql_query($query, $connect);

				                            	$errorDetailsRow = mysql_fetch_assoc($errorDetails);

				                            	$herrorDesc = $errorDetailsRow['error_desc'];

					                    	}

					                    	

											if ($verror != 0) {

					                    		$query = "select error_desc from zt_game_player_error

					                    			where error_id = ".$verror;

				                            	$errorDetails = mysql_query($query, $connect);

				                            	$errorDetailsRow = mysql_fetch_assoc($errorDetails);

				                            	$verrorDesc = $errorDetailsRow['error_desc'];

					                    	}

					                    	

					                    	if ( !empty($herrorDesc) || !empty($verrorDesc) ) {

					                    ?>

					                    <tr>

					                    	<td>&nbsp;</td>

					                    <?php

						                    	if ( !empty($herrorDesc) ) {

						                    		echo "<td colspan=\"4\" class=\"gameError\">".$herrorDesc."</td>";

						                    	} else {

						                    		echo "<td colspan=\"4\">&nbsp;</td>";

						                    	}

						                    	

												if ( !empty($verrorDesc) ) {

						                    		echo "<td colspan=\"4\" class=\"gameError\">".$verrorDesc."</td>";

						                    	} else {

						                    		echo "<td colspan=\"4\">&nbsp;</td>";

						                    	}

					                    ?>

					                    </tr>

					                    <?php

					                    	}

					                    

					                    }

									}

								} else {

					                    ?>

					                        <tr>

					        					<td colspan="9">&nbsp;</td>

					        				</tr>

					                        <tr>

					        					<td colspan="9" align="center"><strong>Nog geen resultaten ontvangen.</strong></td>

					        				</tr>

					                    <?php

					                      }

    								?>

  								</table>

  							</td>

							</tr>

						</table>

					</td>

				</tr>

				<tr>

			</table>

		</td>

	</tr>

</table>



<?php include("footer.php"); ?>